﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MobileBroadbandUserBlog.MBLM;
using MobileBroadbandUserBlog.MBLM.Core.Interfaces;

namespace MobileBroadbandUserBlog.MBLM.Core
{
    public class DirectoryWatcher : IDirectoryWatcher
    {
        FileSystemWatcher _watcher;
        private FileChangeDelegate _delegateToInvoke;

        #region IDirectoryWatcher Members

        public override void StartWatcher(string directory, string filter, FileChangeDelegate delegateToInvoke)
        {
            _delegateToInvoke = delegateToInvoke;

            _watcher = new FileSystemWatcher(directory, filter);
            _watcher.Changed += new FileSystemEventHandler(_watcher_Changed);
            _watcher.NotifyFilter = NotifyFilters.Size;
            _watcher.EnableRaisingEvents = true;
        }

        void _watcher_Changed(object sender, FileSystemEventArgs e)
        {
            this._delegateToInvoke(e.FullPath, e.ChangeType);
        }

        public override void StopWatcher()
        {
            if (_watcher != null)
            {
                _delegateToInvoke = null;
                _watcher.Dispose();
                _watcher = null;
            }

        }

        #endregion

    }
}
